﻿using System.Windows;
using System.Windows.Controls;

namespace JY.Wpf.Controls.Controls
{
    /// <summary>
    /// 切换前后内容控件
    /// </summary>
    public class Flipper : TransitionPresenter.TransitionPresenter
    {
        public Flipper()
        {
            this.Loaded += (s, e) =>
            {
                if (this.IsFlipped)
                {
                    this.Content = this.BackContent;
                    this.ContentTemplate = this.BackContentTemplate;
                    this.ContentTemplateSelector = this.BackContentTemplateSelector;
                }
                else
                {
                    this.Content = this.FrontContent;
                    this.ContentTemplate = this.FrontContentTemplate;
                    this.ContentTemplateSelector = this.FrontContentTemplateSelector;
                }
            };
        }
        public static readonly DependencyProperty FrontContentProperty = DependencyProperty.Register(
            nameof(FrontContent), typeof(object), typeof(Flipper), new PropertyMetadata(default(object)));

        public object FrontContent
        {
            get { return GetValue(FrontContentProperty); }
            set { SetValue(FrontContentProperty, value); }
        }

        public static readonly DependencyProperty FrontContentTemplateProperty = DependencyProperty.Register(
            nameof(FrontContentTemplate), typeof(DataTemplate), typeof(Flipper),
            new PropertyMetadata(default(DataTemplate)));

        public DataTemplate FrontContentTemplate
        {
            get { return (DataTemplate) GetValue(FrontContentTemplateProperty); }
            set { SetValue(FrontContentTemplateProperty, value); }
        }

        public static readonly DependencyProperty FrontContentTemplateSelectorProperty = DependencyProperty.Register(
            nameof(FrontContentTemplateSelector), typeof(DataTemplateSelector), typeof(Flipper),
            new PropertyMetadata(default(DataTemplateSelector)));

        public DataTemplateSelector FrontContentTemplateSelector
        {
            get { return (DataTemplateSelector) GetValue(FrontContentTemplateSelectorProperty); }
            set { SetValue(FrontContentTemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty BackContentProperty = DependencyProperty.Register(
            nameof(BackContent), typeof(object), typeof(Flipper), new PropertyMetadata(default(object)));

        public object BackContent
        {
            get { return GetValue(BackContentProperty); }
            set { SetValue(BackContentProperty, value); }
        }

        public static readonly DependencyProperty BackContentTemplateProperty = DependencyProperty.Register(
            nameof(BackContentTemplate), typeof(DataTemplate), typeof(Flipper),
            new PropertyMetadata(default(DataTemplate)));

        public DataTemplate BackContentTemplate
        {
            get { return (DataTemplate) GetValue(BackContentTemplateProperty); }
            set { SetValue(BackContentTemplateProperty, value); }
        }

        public static readonly DependencyProperty BackContentTemplateSelectorProperty = DependencyProperty.Register(
            nameof(BackContentTemplateSelector), typeof(DataTemplateSelector), typeof(Flipper),
            new PropertyMetadata(default(DataTemplateSelector)));

        public DataTemplateSelector BackContentTemplateSelector
        {
            get { return (DataTemplateSelector) GetValue(BackContentTemplateSelectorProperty); }
            set { SetValue(BackContentTemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty IsFlippedProperty = DependencyProperty.Register(
            nameof(IsFlipped), typeof(bool), typeof(Flipper), new PropertyMetadata(default(bool), IsFlippedPropertyChangedCallback));

        private static void IsFlippedPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var flipper = (Flipper)dependencyObject;
            if (flipper.IsFlipped)
            {
                flipper.Content = flipper.BackContent;
                flipper.ContentTemplate = flipper.BackContentTemplate;
                flipper.ContentTemplateSelector = flipper.BackContentTemplateSelector;
            }
            else
            {
                flipper.Content = flipper.FrontContent;
                flipper.ContentTemplate = flipper.FrontContentTemplate;
                flipper.ContentTemplateSelector = flipper.FrontContentTemplateSelector;
            }
        }

        public bool IsFlipped
        {
            get { return (bool)GetValue(IsFlippedProperty); }
            set { SetValue(IsFlippedProperty, value); }
        }
    }
}
